/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 9/8/12
 * Time: 7:34 PM
 * To change this template use File | Settings | File Templates.
 */
package org.kolonitsky.alexey.utils
{

    public class NumberUtil
    {
        public static function toW3CTime(date:Number):String
        {
            var hour:Number = 0;
            var min:Number = 0;
            var sec:Number = 0;

            if (date > DateUtil.MILISECONDS_PER_HOUR)
            {
                hour = Math.floor(date / DateUtil.MILISECONDS_PER_HOUR);
                date = date % DateUtil.MILISECONDS_PER_HOUR;
            }

            if (date > DateUtil.MILISECONDS_PER_MIN)
            {
                min = Math.floor(date / DateUtil.MILISECONDS_PER_MIN);
                date = date % DateUtil.MILISECONDS_PER_MIN;
            }

            if (date > 1000)
            {
                sec = Math.floor(date / 1000);
                date = date % 1000;
            }

            var result:String = "";
            if (hour || result)
                result += String(hour) + ":";

            if (min)
                result += String(min < 10 && hour ? "0" + min : min) + ":";
            else if (hour)
                result += "00:";

            if (sec || result)
                result += String(sec < 10 && min ? "0" + sec : sec);
            else if (min)
                result += "00";

            return result;
        }

        public static function compare(a:Number, b:Number, p:Number):int
        {
            var d:Number = a - b;
            if (Math.abs(d) < p)
                return 0;

            return (d < 0 ? -1 : 1);
        }
    }
}
